const Sequelize = require('sequelize')
const {sequelize} = require('@/mysql/utils')
const {Project} = require('@/mysql/project')

// 创建用户实体
const Task = sequelize.define('tasks', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: Sequelize.STRING,
            allowNull: false
        },
        description: {
            type: Sequelize.TEXT
        },
        status: {
            type: Sequelize.INTEGER,
            defaultValue: 0
        },
        expire_time: {
            type: Sequelize.DATE(6)
        },
        create_user: {
            type: Sequelize.STRING
        },
        update_user: {
            type: Sequelize.STRING
        },
        forUser: {
            type: Sequelize.STRING
        },
        deleted: {
            type: Sequelize.BOOLEAN,
            defaultValue: false
        }
    }
);

Task.belongsTo(Project)
Task.sync({force: false})
// /**插入新项目类型数据（添加） */
Task.insertData = async (project_id, name, description, forUser, expire_time) => {
    await Task.sync({force: false})
    // 表已创建
    return Task.create({name, description, projectProjectId: project_id, forUser, expire_time})
}

/** 更新项目数据 */
Task.updateData = async (id, name, description, expire_time, forUser) => {
    return Task.update({
        name, description, expire_time, forUser
    }, {
        where: {id}
    })
}
// 查找项目分类类型
Task.findById = async (type_id) => {
    return Task.findAll({where: {type_id}})
}
/** 查找所有项目任务 */
Task.getAllTasks = async (projectProjectId, deleted = false) => {
    return Task.findAll({where: {projectProjectId, deleted}})
}

export {Task}

